<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/11
 * Time: 17:12
 */

namespace Home\Controller;

use Think\Controller;
class CollectController extends Controller{
    public function showCollect(){
        $userInfo=session("UserInfo");
        $collect=D('Collect');
//        var_dump($_POST);
//        var_dump($_GET);
        if(empty($_POST)&&empty($_GET)){
            session('collectSearch',' ');
            session('collectType',0);
            session('collectTime',100);
        }
        else if (!empty($_POST)) {//如果搜索框有内容，则提取内容
            if($_POST['collectSearch']==NULL){
                session('collectSearch',' ');//如果内容为空，则设置未空格字符串
            }else {
                session('collectSearch', $_POST['collectSearch']);//如果内容不为空，则设置搜索框中的内容
            }
        } else if (!empty($_GET)) {//时间选择
            if($_GET['collectTime']==NULL && session('collectTime')==NULL){
                session('collectTime',100);
            }else if($_GET['collectTime']!=NULL){
                session('collectTime',$_GET['collectTime']);//time值赋值为新的get内的值
            }
            if($_GET['collectType']==NULL && session('collectType')==NULL){
                session('collectType',0);
            }else if($_GET['collectType']!=NULL){//如果重新选择拼购类型，则把时间筛选重置为全部
                session('collectType',$_GET['collectType']);
                session('collectTime',100);
            }
            session('collectSearch',' ');
        }
        $collectInfo=$collect->getAllCollect($userInfo['id']);
        $turn=count($collectInfo);
        $collectController=A("Collect");
        //var_dump(session('collectType'));
        //var_dump($collectInfo);
        $collectInfo=$collectController->findByType($collectInfo,session('collectType'),$turn);
        if(session('collectSearch')!=' '){
            $orderController=A("OrderInfo");
            $collectInfo=$orderController->orderSearch($collectInfo,session('collectSearch'),$turn);
        }
        //var_dump($collectInfo);
        $collectInfo=$collectController->orderTimeFilter($collectInfo,session('collectTime'),$turn);
        //var_dump($collectInfo);
        $this->assign('collectInfo',$collectInfo);
        $this->display("User/collect");
    }
    public function delCollect(){//get里的数据为收藏的id，模板中为：{$v['collectid']}
//        $get=array(
//            'id'=>2,
//            '23'=>3,
//        );
        $cart=D("Collect");
        if(!empty($_GET)){
            $cart->deleteByID($_GET);
            echo json_encode($_GET);
        }
        $this->redirect('Collect/showCollect',array(),0,'');
    }

    public function addCollect(){//�û����ӵ��ղؼ�
        $userInfo=session("UserInfo");
        $collect=D("Collect");
        $data=array(
            'user_ID'=>$userInfo['id'],
            'collectDate'=>date('Y-m-d h:i:s'),
        );
        $data=array_merge($data,$_GET);
        if(!empty($_GET)){
            $result = $collect->addToCollect($data);
            echo json_encode($result);
        }
    }
    public function orderTimeFilter($collectInfo, $time,$turn)
    {
        //$time如果是N个月则为整数N，如果是全部，则为100
        for ($i = 0; $i <= $turn; $i++) {
            $info = $collectInfo[$i];
            $times=date('Y-m-d h:i:s', time() - $time * 30 * 86400);
            if ($info['collectdate'] <$times ) {
                unset($collectInfo[$i]);
            }
        }
        return $collectInfo;
    }
    private function findByType($collectInfo,$type,$turn){
        if($type==1){
            for($i=0;$i<$turn;$i++){
                $temp=$collectInfo[$i];
                if($temp['isbingo']!=1){
//                    var_dump($temp['isbingo']);
                    unset($collectInfo[$i]);
                }
            }
        }else if($type==2){
            for($i=0;$i<$turn;$i++){
                $temp=$collectInfo[$i];
                if($temp['isbingo']!=0){
                    unset($collectInfo[$i]);
                }
            }
        }
        return $collectInfo;
    }
}